package edu.ynu.se.xiecheng.achitectureclass.controller;

import edu.ynu.se.xiecheng.achitectureclass.common.controller.LogicController;
import edu.ynu.se.xiecheng.achitectureclass.dao.SoilSensorDao;
import edu.ynu.se.xiecheng.achitectureclass.dto.SoilSensorDTO;
import edu.ynu.se.xiecheng.achitectureclass.entity.SoilSensor;
import edu.ynu.se.xiecheng.achitectureclass.service.SoilSensorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/api/sensor")
@Api(tags = "土壤传感器接口")
public class SoilSensorController extends LogicController<SoilSensorService, SoilSensorDao,SoilSensor,Long> {
    public SoilSensorController(@Autowired SoilSensorService service) {super(service);}

    @PostMapping
    public Boolean refreshSensorData() {
        try {
            return super.getService().refreshData();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @GetMapping
    public List<SoilSensorDTO> getAllSensorsDTO() {
        return super.getService().getAllSoilSensorsDTO();
    }

    @GetMapping("/latest")
    @ApiOperation("获取每种类型传感器的最新数据")
    public List<SoilSensorDTO> getLatestDataForEachSensor() {
        return super.getService().getLatestDataForEachSensor();
    }

    @GetMapping("/moisture/history")
    @ApiOperation("获取土壤含水量的所有历史记录")
    public ResponseEntity<List<SoilSensorDTO>> getSoilMoistureHistory() {
        return ResponseEntity.ok(super.getService().getSoilMoistureHistory());
    }

    @GetMapping("/temperature/history")
    @ApiOperation("获取土壤温度的所有历史记录")
    public ResponseEntity<List<SoilSensorDTO>> getSoilTemperatureHistory() {
        return ResponseEntity.ok(super.getService().getSoilTemperatureHistory());
    }

    @GetMapping("/nitrogen/history")
    @ApiOperation("获取土壤氮含量的所有历史记录")
    public ResponseEntity<List<SoilSensorDTO>> getSoilNitrogenHistory() {
        return ResponseEntity.ok(super.getService().getSoilNitrogenHistory());
    }

    @GetMapping("/phosphorus/history")
    @ApiOperation("获取土壤磷含量的所有历史记录")
    public ResponseEntity<List<SoilSensorDTO>> getSoilPhosphorusHistory() {
        return ResponseEntity.ok(super.getService().getSoilPhosphorusHistory());
    }

    @GetMapping("/potassium/history")
    @ApiOperation("获取土壤钾含量的所有历史记录")
    public ResponseEntity<List<SoilSensorDTO>> getSoilPotassiumHistory() {
        return ResponseEntity.ok(super.getService().getSoilPotassiumHistory());
    }
}
